# -*- coding: utf-8 -*-
import sys

from PyQt4 import QtCore, QtGui, QtWebKit 
from PyQt4.QtCore import Qt
from gui import web_browser_gui

class httpWidget(QtGui.QWidget):

	def __init__(self, parent=None):
		super(httpWidget, self).__init__(parent)
		self.ui = web_browser_gui.Ui_HttpWidget()
		
		self.ui.setupUi(self)
		self.ui.tab_widget.currentWidget().webview.settings().setAttribute(QtWebKit.QWebSettings.PluginsEnabled, True)
		self.ui.tab_widget.currentWidget().webview.settings().setAttribute(QtWebKit.QWebSettings.JavascriptEnabled, True)
		self.ui.tab_widget.currentWidget().webview.settings().setAttribute(QtWebKit.QWebSettings.JavascriptCanOpenWindows, True)
		self.ui.tab_widget.currentWidget().webview.settings().setAttribute(QtWebKit.QWebSettings.JavascriptCanAccessClipboard, True)
		# set margins
		l = self.layout()
		l.setMargin(0)
		self.ui.horizontalLayout.setMargin(5)
		
		# set the default
		self.first_url = 'http://www.google.com'
		self.ui.url.setText(self.first_url)
		
		# load page
		self.ui.tab_widget.currentWidget().webview.setUrl(QtCore.QUrl(self.first_url))
		
		# history buttons:
		self.ui.back.setEnabled(False)
		self.ui.next.setEnabled(False)
		
		self.grabKeyboard()
		
		QtCore.QObject.connect(self.ui.back,QtCore.SIGNAL("clicked()"), self.back)
		QtCore.QObject.connect(self.ui.next,QtCore.SIGNAL("clicked()"), self.next)
		QtCore.QObject.connect(self.ui.url,QtCore.SIGNAL("returnPressed()"), self.url_changed)
		QtCore.QObject.connect(self.ui.tab_widget.currentWidget().webview,QtCore.SIGNAL("linkClicked (const QUrl&)"), self.link_clicked)
		QtCore.QObject.connect(self.ui.tab_widget.currentWidget().webview,QtCore.SIGNAL("urlChanged (const QUrl&)"), self.link_clicked)
		QtCore.QObject.connect(self.ui.tab_widget.currentWidget().webview,QtCore.SIGNAL("loadProgress (int)"), self.load_progress)
		QtCore.QObject.connect(self.ui.tab_widget.currentWidget().webview,QtCore.SIGNAL("titleChanged (const QString&)"), self.title_changed)
		QtCore.QObject.connect(self.ui.reload,QtCore.SIGNAL("clicked()"), self.reload_page)
		QtCore.QObject.connect(self.ui.stop,QtCore.SIGNAL("clicked()"), self.stop_page)
		QtCore.QObject.connect(self.ui.home,QtCore.SIGNAL("clicked()"), self.home)
		QtCore.QObject.connect(self.ui.tab_widget, QtCore.SIGNAL("tabCloseRequested(int)"), self.closeTab)
		QtCore.QObject.connect(self.ui.cytoscape, QtCore.SIGNAL("clicked()"), self.load_cytoscape)
		QtCore.QMetaObject.connectSlotsByName(self)
	
	
	def load_cytoscape(self):
		"""
		String represents the graph which is going to be drawn
		"""
		self.ui.tab_widget.currentWidget().webview.load(QtCore.QUrl("controller\\test.html"))
	
	def closeTab(self, int):
		self.ui.tab_widget.removeTab(int)
	
	def keyPressEvent(self, event):
		self.releaseKeyboard()
		if event.key() == Qt.Key_Plus:
			tab = web_browser_gui.WebTab()
			self.ui.tab_widget.addTab(tab, "new tab")
			self.ui.tab_widget.setCurrentWidget(tab)
			
	def home(self):
		self.ui.tab_widget.currentWidget().webview.setUrl(QtCore.QUrl(self.first_url))
		
	def url_changed(self):
		"""
		Url have been changed by user
		"""
		page = self.ui.tab_widget.currentWidget().webview.page()
		history = page.history()
		if history.canGoBack():
			self.ui.back.setEnabled(True)
		else:
			self.ui.back.setEnabled(False)
		if history.canGoForward():
			self.ui.next.setEnabled(True)
		else:
			self.ui.next.setEnabled(False)
		
		url = self.ui.url.text()
		self.ui.tab_widget.currentWidget().webview.setUrl(QtCore.QUrl(url))
		
	def stop_page(self):
		"""
		Stop loading the page
		"""
		self.ui.tab_widget.currentWidget().webview.stop()
	
	def title_changed(self, title):
		"""
		Web page title changed - change the tab name
		"""
		self.ui.tab_widget.setTabText(self.ui.tab_widget.currentIndex(), title)
		#self.setWindowTitle(title)
	
	def reload_page(self):
		"""
		Reload the web page
		"""
		self.ui.tab_widget.currentWidget().webview.setUrl(QtCore.QUrl(self.ui.url.text()))
	
	def link_clicked(self, url):
		"""
		Update the URL if a link on a web page is clicked
		"""
		page = self.ui.tab_widget.currentWidget().webview.page()
		history = page.history()
		if history.canGoBack():
			self.ui.back.setEnabled(True)
		else:
			self.ui.back.setEnabled(False)
		if history.canGoForward():
			self.ui.next.setEnabled(True)
		else:
			self.ui.next.setEnabled(False)
		
		self.ui.url.setText(url.toString())
	
	def load_progress(self, load):
		"""
		Page load progress
		"""
		if load == 100:
			self.ui.stop.setEnabled(False)
		else:
			self.ui.stop.setEnabled(True)
		
	def back(self):
		"""
		Back button clicked, go one page back
		"""
		page = self.ui.tab_widget.currentWidget().webview.page()
		history = page.history()
		history.back()
		if history.canGoBack():
			self.ui.back.setEnabled(True)
		else:
			self.ui.back.setEnabled(False)
	
	def next(self):
		"""
		Next button clicked, go to next page
		"""
		page = self.ui.tab_widget.currentWidget().webview.page()
		history = page.history()
		history.forward()
		if history.canGoForward():
			self.ui.next.setEnabled(True)
		else:
			self.ui.next.setEnabled(False)

if __name__ == "__main__":
	app = QtGui.QApplication(sys.argv)
	myapp = httpWidget()
	myapp.show()
	sys.exit(app.exec_())
